package ge.unihack.instantdj.protocol.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONException;
import org.json.JSONObject;

public class JSONClientReader implements ClientReader {

	@Override
	public Client readClient(InputStream in) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		String clientData = br.readLine();
		if (clientData == null) {
			return null;
		}
		try {
			JSONObject jsonClient = new JSONObject(clientData);
			Client client = new Client();
			client.setId(jsonClient.getInt("id"));
			return client;
		} catch (JSONException e) {
			throw new IOException("Error while reading JSONClient: " + e.toString());
		}
	}

}
